package com.moonlight.mina;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
//@Component
public class MinaRunning implements CommandLineRunner{
	private final static Logger logger = LoggerFactory.getLogger(MinaRunning.class);
	
	private final static int PORT = 3000;

	@Override
	public void run(String... args) throws Exception {
		// TODO Auto-generated method stub
		NioSocketAcceptor acceptor = new NioSocketAcceptor();
		acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
		acceptor.getSessionConfig().setSendBufferSize(1024);
		acceptor.getSessionConfig().setReceiveBufferSize(1024);
		acceptor.getFilterChain().addLast("codec" , new ProtocolCodecFilter(new MinaEncodingFactory(Charset.forName("utf-8"))));
		acceptor.setHandler(new MinaHandler());
		acceptor.bind(new InetSocketAddress(PORT));
		logger.info("端口3000已经开始监听");
	}
	
}
